package com.wangsd.web.coupon.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.enums.ResultCode;
import com.wangsd.web.MyController;
import com.wangsd.web.coupon.entity.Company;
import com.wangsd.web.coupon.entity.Merchant;
import com.wangsd.web.coupon.entity.Store;
import com.wangsd.web.coupon.service.MerchantService;
import com.wangsd.web.coupon.service.StoreService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

import static com.wangsd.common.enums.ResultCode.PRARM_NOT_NULL;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-20
 */
@Api(value = "merchant", tags = "商户管理")
@RestController
@RequestMapping("/coupon/merchant")
public class MerchantController extends MyController {
    @Autowired
    MerchantService merchantService;
    @Autowired
    StoreService storeService;

    @ApiOperation(value = "查询商户列表", notes = "查询商户列表", httpMethod = "GET")
    @PostMapping("/selectList/{pageNum}/{pageSize}")
    public Result selectList(@ApiParam(value = "当前页", required = true) @PathVariable Integer pageNum,
                             @ApiParam(value = "每页大小", required = true) @PathVariable Integer pageSize,
                             @RequestBody @ApiParam(name = "merchant", value = "传入json格式") Merchant merchant) {
        SysUser userInfo = getUserInfo();
        IPage<Merchant> merchantIPage = merchantService.selectPage(userInfo.getCode(), pageNum, pageSize, merchant);
        return Result.success(merchantIPage);

    }

    @ApiOperation(value = "删除商户", notes = "删除商户", httpMethod = "GET", response = Result.class)
    @GetMapping("/delete/{id}")
    public Result delete(@ApiParam(value = "商户id", required = true) @PathVariable Long id) {
        List<Store> storeList = storeService.list(new QueryWrapper<Store>().eq("merchant_id", id));
        if (storeList != null && storeList.size() > 0) {
            return Result.fail("请先删除当前商户下的门店");
        }
        boolean b = merchantService.removeById(id);
        return Result.success(b);
    }

    @ApiOperation(value = "新增或修改商户信息", notes = "新增或修改商户信息", httpMethod = "POST", response = Result.class)
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Merchant merchant) {

        if (merchant.getCompanyId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "商圈" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(merchant.getName())) {
            return new Result(PRARM_NOT_NULL.getCode(), "商户名称" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(merchant.getAddress())) {
            return new Result(PRARM_NOT_NULL.getCode(), "商户地址" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(merchant.getMobile())) {
            return new Result(PRARM_NOT_NULL.getCode(), "商户电话" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(merchant.getContact())) {
            return new Result(PRARM_NOT_NULL.getCode(), "联系人" + PRARM_NOT_NULL.getMessage());
        }


        if (StringUtils.isBlank(merchant.getSubMchId())) {
            return new Result(PRARM_NOT_NULL.getCode(), "子商户号" + PRARM_NOT_NULL.getMessage());
        }

        Merchant repeatMerchant = merchantService.selectRepeat(merchant.getName(), merchant.getId(),merchant.getCompanyId());
        if (repeatMerchant != null) {
            return Result.message(ResultCode.NAME_REPEAT);
        }

        merchantService.addOrUpdate(merchant);
        return Result.success();

    }

    @ApiOperation(value = "查询所有商户", notes = "查询所有商户", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public Result selectAll() {
        SysUser loginUser = getUserInfo();
        List<Map> list = merchantService.selectAllByUserCode(loginUser.getCode());
        return Result.success(list);
    }

    @ApiOperation(value = "通过商圈id查询所有商户下门店树形结构", notes = "通过商圈id查询所有商户下门店树形结构", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/selectByCompanyId/{companyId}", method = RequestMethod.GET)
    public Result selectByCompanyId(@ApiParam(name = "companyId", value = "商圈（公司id）") @PathVariable Long companyId) {
        JSONArray result = merchantService.selectByCompanyId(companyId);
        return Result.success(result);
    }

    @ApiOperation(value = "通过商圈id查所有商户", notes = "通过商圈查询所有商户", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/selectMerchantByCompanyId/{companyId}", method = RequestMethod.GET)
    public Result selectMerchantByCompanyId(@ApiParam(name = "companyId", value = "商圈（公司id）") @PathVariable Long companyId) {
        List<Merchant> result = merchantService.selectMerchantByCompanyId(companyId);
        return Result.success(result);
    }

}

